using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class BrandUser
	{
		private const string TAG = "BrandUser";
		public string strUsrName;
		public string strNickName;
		public string strAlias;
		public string strBrandIconURL;
		public uint nHidden;
		public static List<BrandUser> fromXml(string strBrandList)
		{
			if (string.IsNullOrEmpty(strBrandList))
			{
				return null;
			}
			List<BrandUser> result;
			try
			{
				XDocument xDocument = XDocument.Parse(strBrandList);
				if (xDocument != null && !(xDocument.get_Root().get_Name() != "brandlist"))
				{
					List<BrandUser> list = new List<BrandUser>();
					using (IEnumerator<XElement> enumerator = xDocument.get_Root().Elements().GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							XElement current = enumerator.get_Current();
							if (!current.get_IsEmpty())
							{
								list.Add(BrandUser.fromXml(current));
							}
						}
					}
					result = list;
				}
				else
				{
					Log.smethod_1("BrandUser", "error parse brand list");
					result = null;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("BrandUser", string.Concat(new object[]
				{
					"parse  strBrandList error=",
					ex,
					"brand list =",
					strBrandList
				}));
				result = null;
			}
			return result;
		}
		public static BrandUser fromXml(XElement item)
		{
			BrandUser brandUser = new BrandUser();
			BrandUser result;
			using (IEnumerator<XElement> enumerator = item.Elements().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					XElement current = enumerator.get_Current();
					if (current.get_Value() == null)
					{
						result = null;
						return result;
					}
					if (current.get_Name() == "username")
					{
						brandUser.strUsrName = (string)current;
					}
					else
					{
						if (current.get_Name() == "nickname")
						{
							brandUser.strNickName = (string)current;
						}
						else
						{
							if (current.get_Name() == "alias")
							{
								brandUser.strAlias = (string)current;
							}
							else
							{
								if (current.get_Name() == "iconurl")
								{
									brandUser.strBrandIconURL = (string)current;
								}
								else
								{
									if (current.get_Name() == "Hidden")
									{
										brandUser.nHidden = (uint)current;
									}
								}
							}
						}
					}
				}
				return brandUser;
			}
			return result;
		}
		public Contact ToContact()
		{
			return new Contact
			{
				strUsrName = this.strUsrName,
				strBrandIconURL = this.strBrandIconURL
			};
		}
		public static XElement toXml(BrandUser user)
		{
			XElement xElement = new XElement("brand");
			xElement.Add(new XElement("username", user.strUsrName));
			xElement.Add(new XElement("nickname", user.strNickName));
			xElement.Add(new XElement("alias", user.strAlias));
			xElement.Add(new XElement("iconurl", user.strBrandIconURL));
			xElement.Add(new XElement("Hidden", user.nHidden));
			return xElement;
		}
		public static string toXml(List<BrandUser> userList)
		{
			if (userList != null && userList.get_Count() != 0)
			{
				XElement xElement = new XElement("brandlist");
				using (List<BrandUser>.Enumerator enumerator = userList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						BrandUser current = enumerator.get_Current();
						xElement.Add(BrandUser.toXml(current));
					}
				}
				StringWriter stringWriter = new StringWriter();
				xElement.Save(stringWriter);
				return stringWriter.GetStringBuilder().ToString();
			}
			return null;
		}
	}
}
